using System;
using System.Collections;
using UnityEngine;
using QFramework;
using Sirenix.OdinInspector;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;

namespace Survivor
{
	public partial class Enemy : ViewController
	{
		[Header("基本属性")]
		public int hp = 3;
		public float movementSpeed = 2f;
		[Header("攻击")] 
		private float _currentSeconds = 0;

		[FormerlySerializedAs("damege")] public int damage = 1;
		public float attackInterval = 1f;
		[HideInInspector] public float attackRange = 1f;  // 攻击范围
		[HideInInspector] public LayerMask attackTargetLayer;

		public bool canMove = true;

		void Start()
		{
			EnemyGenerator.EnemyNum.Value+=1;
			_currentSeconds = 0;
			
			//todo:攻击范围暂时和碰撞箱保持一致，有时间做更复杂的敌人的时候再改
			attackRange = HitBox.GetComponent<CircleCollider2D>().radius;
			
			//todo:攻击目标暂时只有Player，有时间做更复杂的逻辑再改
			// 用NameToLayer，不知道为什么会没用，明明值都是对的
			attackTargetLayer = LayerMask.GetMask("Player");
		}

		private void OnDestroy()
		{
			EnemyGenerator.EnemyNum.Value -= 1;
		}

		private void Update()
		{
			var player = Player.Default;
			if (player && canMove)
			{
				var direction = (player.transform.position - transform.position).normalized;
				transform.Translate(direction * (Time.deltaTime * movementSpeed));
			}
			Attack();

			if (hp <= 0)
			{
				this.DestroyGameObjGracefully();
				DropItemManager.Instance.GenerateRandomDropItem(transform.position);
			}
		}

		public void InitHp(int value)
		{
			hp = value;
		}

		public void Attack()
		{
			_currentSeconds += Time.deltaTime;
			if (_currentSeconds >= attackInterval)
			{
				var col = Physics2D.OverlapCircle(transform.position, attackRange,attackTargetLayer);
				if (col)
				{
					var player = col.gameObject.GetComponentInParent<Player>();
					if (player)
					{
						player.Hurt(damage);
						_currentSeconds = 0;
					}
				}
			}
		}

		private bool _isHurtPerforming; //正在做受伤表现
		private float _hurtPerformDuration;  // 变红的持续时间
		public void Hurt(int damage)
		{
			bool isCritical = false;
			(damage,isCritical) = DamageSystem.CalculateDamage(damage);
			hp -= damage;
			AudioKit.PlaySound("Hit");
			var floatingDirection = new Vector3(Random.Range(-0.5f, 0.5f), 0.5f, 0);
			FloatingTextController.Play(transform.position+new Vector3(Random.Range(-1f, 1f), 2f, 0),
				damage.ToString(),floatingDirection,
				isCritical);
			if (_isHurtPerforming)
			{
				_hurtPerformDuration = 0.1f;
			}
			else
			{
				StartCoroutine(IHurtPerform());
			}
		}

		private IEnumerator IHurtPerform()
		{
			canMove = false;
			_hurtPerformDuration = 0.1f;
			Sprite.color = Color.red;
			
			while (_hurtPerformDuration > 0)
			{
				_hurtPerformDuration -= Time.deltaTime;
				yield return null;
			}

			_isHurtPerforming = false;
			Sprite.color = Color.white;
			canMove = true;
		}

		private void OnDrawGizmos()
		{
			Gizmos.color = Color.red;
			Gizmos.DrawWireSphere(transform.position,attackRange);
		}
	}
}
